<?php
class ComponentController extends AppController
{
    public $name = "Component";
    public $uses = array(
        'Advertisement',
        'Contact',
        'Gallery',
        'Link',
        'News',
        'NewsCategory',
        'Partner',
        'Product',
        'ProductCategory',
        'Question',
        'Setting',
        'Slideshow',
        'Support',
        'Video',
        'VideoCategory'
    );

    public function beforeFilter()
    {
        $this->autoRender = false;
        $this->autoLayout = false;
        //Configure::write('debug', 0);
    }

    /*
     * funciton get setting of system
     * author: NGuyen tat Loi
     * date: 26/11/2013
     */
    public function get_setting()
    {
        if(!$setting = Cache::read('get_setting')){
            $setting = $this->Setting->find('first');
            Cache::write('get_setting', $setting);
        }
        return $setting;
    }

    /*
     * public function get advertisment of system
     * get by position:
     * vị trí của quảng cáo:
     *      1 - bên trái,
     *      2 - bên phải,
     *      3 - center,
     *      4 - top .... or tùy chọn :D
     * author: NGuyen Tat Loi
     * date: 26/11/2013
     */
    public function get_adv($position = 0)
    {
        //if position = 0, get all advertisment
        if(!$adv = Cache::read('get_adv_'.$position)){
            if($position ==0){
                $adv = $this->Advertisement->find(
                    'all', array(
                        'conditions' => array('Advertisement.status' => 1)
                    )
                );
            }else{
                $adv = $this->Advertisement->find(
                    'all', array(
                        'conditions' => array(
                            'Advertisement.status' => 1,
                            'Advertisement.position' => $position
                        )
                    )
                );
            }
            Cache::write('get_adv_'.$position, $adv);
        }
        return $adv;
    }

    /*
     * function get support online
     * author: Nguyen Tat Loi
     * date: 26/11/2013
     */
    public function get_support()
    {
        if(!$support = Cache::read('get_support')){
            $support = $this->Support->find(
                'all', array(
                    'Support.status' => 1
                )
            );
            Cache::write('get_support', $support);
        }
        return $support;
    }

    /*
     * function get link outer website
     * author: NGuyen Tat Loi
     * date: 26/11/2013
     */
    public function get_link()
    {
        if(!$link = Cache::read('get_link')){
            $link = $this->Link->find(
                'all', array(
                    'conditions' => array(
                        'Link.status' => 1
                    )
                )
            );
            Cache::write('get_link', $link);
        }
        return $link;
    }

    /*
     * function get partner
     * author: Nguyen Tat Loi
     * date: 26/11/2013
     */
    public function get_partner()
    {
        if(!$partner = Cache::read('get_partner')){
            $partner = $this->Partner->find(
                'all', array(
                    'conditions' => array(
                        'Partner.status' => 1
                    )
                )
            );
            Cache::write('get_partner', $partner);
        }
        return $partner;
    }

    /*
     * function get question FAQ
     * author: NGuyen Tat Loi
     * date: 26/11/2013
     */
    public function get_question()
    {
        if(!$question = Cache::read('get_question')){
            $question = $this->Question->find(
                'all', array(
                    'conditions' => array('Question.status' => 1),
                    'limit' => 10,
                    'order' => array('Question.id' => 'DESC')
                )
            );
            Cache::write('get_question', $question);
        }
        return $question;
    }

    /*
     * function get slide show
     * author: NGuyen Tat Loi
     * date: 26/11/2013
     */
    public function get_slideshow()
    {
        if(!$slideshow = Cache::read('get_slide_show')){
            $slideshow = $this->Slideshow->find(
                'all', array(
                    'conditions' => array('Slideshow.status' => 1)
                )
            );
            Cache::write('get_slide_show', $slideshow);
        }
        return $slideshow;
    }

    /*
     * function get product category
     * set product category to tree
     * author: Nguyen Tat Loi
     * date: 7/6/2014
     */
    public function get_product_category()
    {
        if(!$product_category = Cache::read('get_product_category')){
            $product_category = $this->ProductCategory->find('all');
            if(!empty($product_category)){
                $temp = array();
                foreach($product_category as $key => $value){
                    $temp[$value['ProductCategory']['id']] = $value['ProductCategory'];
                }
            }
            $this->treeze($temp, 'parent_id', 'child');
            $product_category = $temp;
            Cache::write('get_product_category', $product_category);
        }
        return $product_category;
    }

    /*
     * function get news category
     * set news category to tree
     * author: NGuyen Tat Loi
     * date: 7/6/2014
     */
    public function get_news_category()
    {
        if(!$news_category = Cache::read('get_news_category')){
            $news_category = $this->NewsCategory->find('all');
            if(!empty($news_category)){
                $temp = array();
                foreach($news_category as $key => $value){
                    $temp[$value['NewsCategory']['id']] = $value['NewsCategory'];
                }
            }
            $this->treeze($temp, 'parent_id', 'child');
            $news_category = $temp;
            Cache::write('get_news_category', $news_category);
        }
        return $news_category;
    }

    /*
     * function get new product
     * author: Nguyen tat Loi
     * date: 3/6/2014
     */
    public function get_new_product($limit = 10)
    {
        if(!$product = Cache::read('get_new_product')){
            $product = $this->Product->find(
                'all', array(
                    'conditions' => array(
                        'Product.status' => 1
                    ),
                    'order' => array('Product.sort_order' => 'ASC'),
                    'limit' => $limit
                )
            );
            Cache::write('get_new_product', $product);
        }
        return $product;
    }

    /*
     * function get hot news
     * author: Nguyen Tat Loi
     * date: 7/6/2014
     */
    public function get_hot_news($limit = 10)
    {
        if(!$news = Cache::read('get_hot_news')){
            $news = $this->News->find(
                'all', array(
                    'order' => array('News.id' => 'DESC'),
                    'limit' => $limit
                )
            );
        }
        return $news;
    }









    /*
     * function render from array to tree
     * author: Nguyen Tat Loi
     * date: 7/6/2014
     */
    private function treeze( &$a, $parent_key, $children_key )
    {
        $orphans = true;
        while( $orphans )
        {
            $orphans = false;
            foreach( $a as $k=>$v )
            {
                // is there $a[$k] sons?
                $sons = false; foreach( $a as $x=>$y ) if( $y[$parent_key]!=false and $y[$parent_key]==$k )  { $sons=true; $orphans=true; break; }

                // $a[$k] is a son, without children, so i can move it
                if( !$sons and $v[$parent_key]!=false )
                {
                    $a[$v[$parent_key]][$children_key][$k] = $v;
                    unset( $a[$k] );
                }
            }
        }
    }




    public function get_menu()
    {
        if(!$menu = Cache::read('get_menu')){
            $menu = $this->ProductCategory->find('all', array('order' => 'sort_order DESC', 'conditions' => array('ProductCategory.parent_id' => 0)));
            if(!empty($menu)){
                foreach($menu as $key => $value){
                    $menu[$key]['product'] = $this->Product->find(
                        'all', array(
                            'fields' => array(
                                'Product.name', 'Product.alias'
                            ),
                            'conditions' => array(
                                'Product.product_category_id' => $value['ProductCategory']['id'],
                                'Product.status' => 1
                            ),
							'order' => array('Product.sort_order' => 'ASC'),
                            'limit' => 10
                        )
                    );
                }
            }
            Cache::write('get_menu', $menu);
        }
        return $menu;
    }
}
?>